    //
//  NetworkTools.swift
//  Status
//
//  Created by 花轮 on 15/5/18.
//  Copyright (c) 2015年 花轮. All rights reserved.
//

import UIKit
import Alamofire
import SVProgressHUD

class NetworkTools: NSObject {
    class func requestJSON(method: Alamofire.Method, URLStr: String, parameters: [String: AnyObject]? = nil, completion:(JSON: AnyObject?)->()) {
    Alamofire.request(method, URLStr, parameters: parameters).responseJSON() { (_, _, JSON, error) in
        
        // 出错处理
        if JSON == nil || error != nil
            {
                SVProgressHUD.showInfoWithStatus("您的网络不给力")
                
                println("ERROR JSON： \(JSON) error： \(error)")
                
                completion(JSON: nil)
                return
            }
        completion(JSON: JSON)
        }
    }
    // ***************************************
    static let boundary = "LK_upload"
    class func uploadPict(URLStr: String, fieldName: String, dataList:[String: NSData!], parameters: [String: AnyObject]? = nil) {
        var request = NSMutableURLRequest(URL: NSURL(string: URLStr)!)

        // 设置请求的方法
        request.HTTPMethod = "POST"
        
        let type = "multipart/form-data; boundary=\(boundary)"
        
        // 设置contentType
        request.setValue(type, forHTTPHeaderField: "Content-Type")
        
        let data = formData(fieldName, dataList: dataList, parameters: parameters)
        
        Alamofire.upload(request, data).responseJSON() { (_, _, JSON, error) -> Void in
            print(JSON)
            println(error)
        }
    }
    
    
    /**
    -----------------------------1979450919738311251999822666 
    Content-Disposition: form-data; name="userfile[]"; filename="abcxt.txt" 
    Content-Type: text/plain 
    
    ÿþ1�1� 
    
    -----------------------------1979450919738311251999822666 
    Content-Disposition: form-data; name="userfile[]"; filename="" 
    Content-Type: application/octet-stream 
    
    -----------------------------1979450919738311251999822666
    Content-Disposition: form-data; name="status"
    
    hi
    -----------------------------1979450919738311251999822666--
    */
    // 生成上传文件数据
    // string 是要上传到服务器的文件名
    // 要上传文件的数据
   
    private class func formData(fieldName: String, dataList:[String: NSData!], parameters: [String: AnyObject]? = nil) -> NSData {
        
        var dataM = NSMutableData()

        // 遍历字典拼接字符串参数
        if let params = parameters
        {
            for (k, v) in params {
                var strM = "--\(boundary)\r\n"
                strM += "Content-Disposition: form-data; name=\"\(k)\"\r\n\r\n"

                strM += "\(v)\r\n"
                dataM.appendData(strM.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
            }
        }
        
        
        // 遍历文件数据数组
        for (k, v) in dataList {
            var strM = "--\(boundary)\r\n"
            strM += "Content-Disposition: form-data; name=\"\(fieldName)\"; filename=\"\(k)\"\r\n"
            strM += "Content-Type: application/octet-stream\r\n\r\n"
            
            dataM.appendData(strM.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
            dataM.appendData(v)
            dataM.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
        }
        
        // 请求结尾字符串
        let tail = "--\(boundary)--\r\n"

        dataM.appendData(tail.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
        
        return dataM
    }
}